﻿<%@ Page 
    Language="C#" 
    AutoEventWireup="true" 
    CodeBehind="Atlas_EntityDataSource.aspx.cs" 
    Inherits="AtlasWebApp.Atlas_EntityDataSource" 
    ErrorPage="~/ErrorPage.aspx"
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Atlas</title>
    <style type="text/css" rel="stylesheer">
        .ImageInDropDownList
        {
            max-width: 30px;
            max-height: 30px;
        }
        .ImageFlag
        {
            max-width: 100px;
            max-height: 100px;
        }
    </style>
</head>
<body>    
    <asp:Literal runat="server" ID="LiteralErrorMessage" />
    <form id="formMain" runat="server">
        <asp:ScriptManager runat="server" ID="ScriptManager" />
    <div>      
        <p>
        <asp:ListBox runat="server" 
                ID="ListBoxContinents"
                DataSourceID="EntityDataSourceContinents" 
                DataTextField="ContinentName" 
                DataValueField="ContinentId" 
                AutoPostBack="true" > 
        </asp:ListBox>
        </p>  
        <asp:ListView runat="server" ID="ListViewContinents" 
                DataSourceID="EntityDataSourceContinents" 
                DataKeyNames="ContinentId" 
                InsertItemPosition="FirstItem" >
            <ItemTemplate>
                <p>
                    <asp:Label runat="server" Text=' <%# Eval("ContinentName") %>' />                
                    <asp:Button runat="server" ID="ButtonEdit" OnClick="ButtonEdit_OnClick" Text="Edit" />
                </p>
            </ItemTemplate>
            <EditItemTemplate>
                <p>                    
                    <asp:TextBox runat="server" ID="TextBoxContinentName" Text=' <%# Bind("ContinentName") %>' />
                    <asp:Button runat="server" ID="ButtonSave" CommandName="Update" OnClick="ButtonSave_OnClick" Text="Save" />
                    <asp:Button runat="server" ID="ButtonDelete" CommandName="Delete" OnClick="ButtonDelete_OnClick" Text="Delete" />
                </p>
            </EditItemTemplate>
            <InsertItemTemplate>
                <p>
                    New Continent's Name
                    <asp:TextBox runat="server" ID="TextBoxNewContinentName" Text=' <%# Bind("ContinentName") %>' />
                    <asp:Button runat="server" ID="ButtonInsert" CommandName="Insert" OnClick="ButtonInsert_OnClick" Text="Insert" />
                </p>
            </InsertItemTemplate>
        </asp:ListView>

        <asp:GridView runat="server" 
                ID="GridViewCountries" 
                DataKeyNames="CountryId" 
                DataSourceID="EntityDataSourceCountries" 
                AutoGenerateColumns="false" 
                AutoGenerateSelectButton="True" 
                AutoGenerateEditButton="true" 
                AutoGenerateDeleteButton="true"                 
                ShowFooter="true" >
            <Columns>            
                <asp:TemplateField HeaderText="Flag">
                    <ItemTemplate>
                        <asp:Image runat="server" CssClass="ImageFlag" ImageUrl='<%# "~/DisplayFlag.ashx?flagId=" + Eval("FlagId") %>' />
                    </ItemTemplate> 

                    <EditItemTemplate>
                        <asp:UpdatePanel runat="server" ID="UpdatePanelFlags">                            
                            <ContentTemplate>
                                <asp:DropDownList runat="server" ID="DropDownListEditFlags" 
                                        DataSourceID="EntityDataSourceFlags" 
                                        DataTextField="FlagId"
                                        DataValueField="FlagId" 
                                        SelectedValue=' <%# Bind("FlagId") %>' 
                                        ImageUrl='<%# "~/DisplayFlag.ashx?flagId=" + Eval("FlagId") %>' 
                                        AutoPostBack="true" >
                                </asp:DropDownList>
                                <asp:EntityDataSource ID="EntityDataSourceSelectedFlag" runat="server" 
                                        ConnectionString="name=AtlasEntities" 
                                        DefaultContainerName="AtlasEntities"             
                                        EntitySetName="Flags"                 
                                        EnableFlattening="False"
                                        Where="it.[FlagId]==@flagId" >                                                   
                                    <WhereParameters>
                                        <asp:ControlParameter ControlID="DropDownListEditFlags" Name="flagId" Type="Int32" />
                                    </WhereParameters>
                                </asp:EntityDataSource>
                                <asp:ListView runat="server" ID="ListViewSelectedFlag" DataSourceID="EntityDataSourceSelectedFlag">
                                    <ItemTemplate>
                                        <%--<asp:Literal runat="server" ID="LiteralSelectedFlag" Text=' <%# Eval("FlagId") %>'  />--%>
                                        <asp:Image CssClass="ImageInDropDownList" ID="ImageFlags_EditCountry" runat="server" ImageUrl='<%# "~/DisplayFlag.ashx?flagId=" + Eval("FlagId") %>' />
                                    </ItemTemplate>
                                </asp:ListView>
                            </ContentTemplate>                            
                        </asp:UpdatePanel>
                    </EditItemTemplate>
                                        
                    <FooterTemplate>
                        <asp:UpdatePanel runat="server" ID="UpdatePanelFlags_Insert">
                            <ContentTemplate >
                                <asp:DropDownList runat="server" ID="DropDownListFlags_Insert" 
                                        DataSourceID="EntityDataSourceFlags" 
                                        DataTextField="FlagId"
                                        DataValueField="FlagId" 
                                        SelectedValue=' <%# Bind("FlagId") %>' 
                                        ImageUrl='<%# "~/DisplayFlag.ashx?flagId=" + Eval("FlagId") %>' 
                                        AutoPostBack="true" >
                                </asp:DropDownList>
                                <asp:EntityDataSource ID="EntityDataSourceSelectedFlag_Insert" runat="server" 
                                        ConnectionString="name=AtlasEntities" 
                                        DefaultContainerName="AtlasEntities"             
                                        EntitySetName="Flags"                 
                                        EnableFlattening="False"
                                        Where="it.[FlagId]==@flagId" >                                                   
                                    <WhereParameters>
                                        <asp:ControlParameter ControlID="DropDownListFlags_Insert" Name="flagId" Type="Int32" />
                                    </WhereParameters>
                                </asp:EntityDataSource>
                                <asp:ListView runat="server" ID="ListViewSelectedFlag_Insert" DataSourceID="EntityDataSourceSelectedFlag_Insert">
                                    <ItemTemplate>
                                        <%--<asp:Literal runat="server" ID="LiteralSelectedFlag" Text=' <%# Eval("FlagId") %>'  />--%>
                                        <asp:Image CssClass="ImageInDropDownList" ID="ImageFlags_EditCountry" runat="server" ImageUrl='<%# "~/DisplayFlag.ashx?flagId=" + Eval("FlagId") %>' />
                                    </ItemTemplate>
                                </asp:ListView>
                            </ContentTemplate>
                        </asp:UpdatePanel>
                    </FooterTemplate>
                </asp:TemplateField>
                                
                <asp:TemplateField HeaderText="Country">
                    <ItemTemplate>
                        <asp:Literal runat="server" ID="LiteralCountryName" Text='<%# Eval("CountryName") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="TextBoxEditCountryName" Text='<%# Bind("CountryName") %>' />
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:Button runat="server" ID="ButtonInsertNewCountry" Text="Create" OnClick="ButtonInsertNewCountry_OnClick" />
                        <asp:TextBox runat="server" ID="TextBoxInsertCountryName" Text='<%# Bind("CountryName") %>' />
                    </FooterTemplate>                                        
                </asp:TemplateField>                

                <%--<asp:TemplateField HeaderText="Country Id">
                    <ItemTemplate>
                        <asp:Literal runat="server" ID="LiteralCountryId" Text='<%# Eval("CountryId") %>' />
                    </ItemTemplate>                    
                </asp:TemplateField>--%>

                <asp:TemplateField HeaderText="Language">
                    <ItemTemplate>
                        <%--N+1 Problem right here and WORSE--%>
                        <asp:Literal runat="server" ID="LiteralLanguage" Text='<%# GetLanguageNameByCountryId(Eval("CountryId")) %>' />
                    </ItemTemplate> 
                    <EditItemTemplate>
                        <asp:DropDownList runat="server" ID="DropDownListEditLanguages" 
                                DataSourceID="EntityDataSourceLanguages" 
                                DataTextField="LanguageName" 
                                DataValueField="LanguageId" 
                                SelectedValue=' <%# Bind("LanguageId") %>' />      
                    </EditItemTemplate>           
                    <FooterTemplate>                        
                        <asp:DropDownList runat="server" ID="DropDownListInsertLanguages" 
                                DataSourceID="EntityDataSourceLanguages" 
                                DataTextField="LanguageName" 
                                DataValueField="LanguageId" 
                                SelectedValue=' <%# Bind("LanguageId") %>' />      
                    </FooterTemplate> 
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Continent">
                    <ItemTemplate>
                        <%--N+1 Problem right here and WORSE--%>
                        <asp:Literal runat="server" ID="LiteralContinent" Text='<%# GetContinentNameByCountryId(Eval("CountryId")) %>' />
                    </ItemTemplate>            
                    <EditItemTemplate>
                        <%--<asp:TextBox runat="server" ID="TextBoxEditContinentId" Text=' <%# Bind("ContinentId") %>' />--%>
                        <asp:DropDownList runat="server" ID="DropDownListEditContinentId" 
                                DataSourceID="EntityDataSourceContinents" 
                                DataTextField="ContinentName" 
                                DataValueField="ContinentId" 
                                SelectedValue=' <%# Bind("ContinentId") %>' />
                    </EditItemTemplate>        
                    <FooterTemplate>                       
                        <asp:DropDownList runat="server" ID="DropDownListInsertContinentId" 
                                DataSourceID="EntityDataSourceContinents" 
                                DataTextField="ContinentName" 
                                DataValueField="ContinentId" 
                                SelectedValue=' <%# Bind("ContinentId") %>' />
                    </FooterTemplate>      
                </asp:TemplateField>                
            </Columns>      
            
        </asp:GridView>

        <asp:ListView runat="server" 
            ID="ListViewTowns"
            DataSourceID="EntityDataSourceTowns">
            <ItemTemplate >
                <p>
                    <asp:Label runat="server" Text=' <%# Eval("TownName") %>' />
                </p>
            </ItemTemplate>
        </asp:ListView>
       
        <asp:EntityDataSource ID="EntityDataSourceContinents" runat="server" 
                ConnectionString="name=AtlasEntities" 
                DefaultContainerName="AtlasEntities"             
                EntitySetName="Continents" 
                EnableInsert="True" 
                EnableUpdate="True"
                EnableDelete="True" 
                EnableFlattening="False" >                                                   
        </asp:EntityDataSource>

        <asp:EntityDataSource ID="EntityDataSourceCountries" runat="server" 
                ConnectionString="name=AtlasEntities" 
                DefaultContainerName="AtlasEntities"             
                EntitySetName="Countries"
                EnableInsert="True" 
                EnableUpdate="True"
                EnableDelete="True" 
                Where="it.[ContinentId]==@continentId" >
            <WhereParameters>
                <asp:ControlParameter ControlID="ListBoxContinents" Type="Int32" Name="continentId" />
            </WhereParameters>
        </asp:EntityDataSource>
        
        <asp:EntityDataSource ID="EntityDataSourceTowns" runat="server" 
                ConnectionString="name=AtlasEntities" 
                DefaultContainerName="AtlasEntities"             
                EntitySetName="Towns"
                EnableInsert="True" 
                EnableUpdate="True"
                EnableDelete="True" 
                Where="it.[CountryId]==@countryId" >
            <WhereParameters>
                <asp:ControlParameter ControlID="GridViewCountries" Type="Int32" Name="countryId" />
            </WhereParameters>
        </asp:EntityDataSource>


        <asp:EntityDataSource ID="EntityDataSourceLanguages" runat="server" 
                ConnectionString="name=AtlasEntities" 
                DefaultContainerName="AtlasEntities"             
                EntitySetName="Languages" 
                EnableInsert="True" 
                EnableUpdate="True"
                EnableDelete="True" 
                EnableFlattening="False" >                                                   
        </asp:EntityDataSource>

        <asp:EntityDataSource ID="EntityDataSourceFlags" runat="server" 
                ConnectionString="name=AtlasEntities" 
                DefaultContainerName="AtlasEntities"             
                EntitySetName="Flags"                 
                EnableFlattening="False" >                                                   
        </asp:EntityDataSource>


       
       <asp:Label runat="server" ID="LabelError" ></asp:Label>
    </div>
    </form>
</body>
</html>
